package mao.leetcode.others;

public class aa {
    public static void main(String[] args) {
        decodeString("3[abba]2[bc]3[b]bbccddxxssssss");
    }

    public static String decodeString(String s) {
        int[] index = {0};
        StringBuilder res = dfs(s,index);
        return res.toString();
    }
    public static StringBuilder dfs(String s,int[] index){
        StringBuilder builder = new StringBuilder();
       // builder.
        int mutil=0;
        while(index[0] < s.length()){
            char c = s.charAt(index[0]);
            if(c == '['){
                index[0]++;
                StringBuilder tmp = dfs(s,index);
                while(mutil > 0){
                    builder.append(tmp);
                    mutil--;
                }
            }else if(c ==']'){
                index[0]++;
                return builder;
            }else if(c >= '0' && c<='9'){
                mutil=mutil*10+c-'0';
            }else{
                builder.append(c);
            }
            index[0]++;
        }
        return builder;
    }
}
